package com.zj.controller;

import com.zj.config.Configure1;
import com.zj.pojo.*;
import com.zj.tool.SpringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@RestController
@RequestMapping("")
public class BeanTestController {
    private static Logger log = LoggerFactory.getLogger(BeanTestController.class);

    @Autowired
    private SpringUtil util;

    //无法得到enablewebmvc一样的效果，外部类Configure1的bean一直不能装入
    @GetMapping("/getbean")
    public Result getBean() {
        Configure1 configure11 = new Configure1();
        ApplicationContext applicationContext = util.getApplicationContext();
//        AppleConf bean1 = applicationContext.getBean(AppleConf.class);
//        FruitConf bean11 = applicationContext.getBean(FruitConf.class);
        Object bean2 = null, bean3 = null, bean4 = null;

        String configInner1 = util.getBeanName("ConfigInnerAdapter1");
        if (!StringUtils.isEmpty(configInner1)) {
            bean2 = util.getBean(configInner1);
        }
        String configInner2 = util.getBeanName("ConfigInner2");
        if (!StringUtils.isEmpty(configInner2)) {
            bean3 = util.getBean(configInner2);
        }
        String configure3 = util.getBeanName("Configure1");
        if (!StringUtils.isEmpty(configure3)) {
            bean4 = util.getBean(configure3);
        }
        System.out.println("aa");

        return new Result(HttpStatus.OK.value(), "query success", null);


    }

    //当加上enableWebMvc时，WebMvcAutoConfiguration相关的3个类均不会放入到容器中，不加时则会全加入。
//    bean1he bean11均为WebMvcAutoConfiguration$EnableWebMvcConfiguration
    @GetMapping("/getSpringBean")
    public Result getSpringBean() {
        ApplicationContext applicationContext = util.getApplicationContext();
        DelegatingWebMvcConfiguration bean1 = applicationContext.getBean(DelegatingWebMvcConfiguration.class);
        WebMvcConfigurationSupport bean11 = applicationContext.getBean(WebMvcConfigurationSupport.class);
        Object bean2 = null, bean3 = null, bean4 = null, bean5 = null;
        String configure1 = util.getBeanName("WebMvcAutoConfigurationAdapter");
        if (!StringUtils.isEmpty(configure1)) {
            bean2 = util.getBean(configure1);
            Class<?> aClass = bean2.getClass();
            Object bean21 = util.getBean(aClass);
            System.out.println(aClass);
        }
        String configInner2 = util.getBeanName("EnableWebMvcConfiguration");
        if (!StringUtils.isEmpty(configInner2)) {
            bean3 = util.getBean(configInner2);
            Class<?> aClass = bean3.getClass();
            Object bean31 = util.getBean(aClass);
            System.out.println(aClass);
        }
        String configInner3 = util.getBeanName("WebMvcAutoConfiguration");
        if (!StringUtils.isEmpty(configInner3)) {
            bean4 = util.getBean(configInner3);
            Class<?> aClass = bean4.getClass();
            Object bean41 = util.getBean(aClass);
            System.out.println(aClass);
        }
        System.out.println("aa");


        System.out.println("aa");

        return new Result(HttpStatus.OK.value(), "query success", null);


    }
}
